<!DOCTYPE html>
<html>

<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>规格管理</title>
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="/plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="/plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="/plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="/css/style.css">
	<script src="/plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="/plugins/bootstrap/js/bootstrap.min.js"></script>
    
</head>

<body class="hold-transition skin-red sidebar-mini" >
  <!-- .box-body -->                
                    <div class="box-header with-border">
                        <h3 class="box-title">规格管理</h3>
                    </div>
                    <div class="box-body">
                        <!-- 数据表格 -->
                        <div class="table-box">

                            <!--工具栏-->
                            <div class="pull-left">
                                <div class="form-group form-inline">
                                    <div class="btn-group">
                                        <button type="button" class="btn btn-default" title="新建" data-toggle="modal" data-target="#editModal" ><i class="fa fa-file-o"></i> 新建</button>
                                        <button type="button" class="btn btn-default" onclick="delSpecificatons()" title="删除" ><i class="fa fa-trash-o"></i> 删除</button>
                                        
                                        <button type="button" class="btn btn-default" title="刷新" onclick="window.location.reload();"><i class="fa fa-refresh"></i> 刷新</button>
                                    </div>
                                </div>
                            </div>
                            <div class="box-tools pull-right">
                                <div class="has-feedback">
							                    规格名称：<input id="specNamee" type="text"  >
									<button onclick="listSpecifications()" class="btn btn-default">查询</button>
                                </div>
                            </div>
                            <!--工具栏/-->
			                  <!--数据列表-->
			                  <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
			                      <thead>
			                          <tr>
			                              <th class="" style="padding-right:0px">
			                                  <input id="selall" type="checkbox" class="icheckbox_square-blue">
			                              </th> 
										  <th class="sorting_asc">规格ID</th>
									      <th class="sorting">规格名称</th>									     												
					                      <th class="text-center">操作</th>
			                          </tr>
			                      </thead>
			                      <tbody id="listSpecifications">

			                      </tbody>
			                  </table>
			                  <!--数据列表/-->    
                        </div>
                        <!-- 数据表格 /--> 
                     </div>
                    <!-- /.box-body -->                    
	          		    
                                
<!-- 编辑窗口 -->
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" >
	<div class="modal-content">
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
			<h3 id="myModalLabel">规格编辑</h3>
		</div>
		<div class="modal-body">							
			
			<table class="table table-bordered table-striped"  width="800px">
		      	<tr>
		      		<td>规格名称</td>
		      		<td><input id="specName" class="form-control" placeholder="规格名称" >  </td>
		      	</tr>
			 </table>
			 <!-- 规格选项 -->
			 <div class="btn-group">
                  <button type="button" onclick="addSpecOptionHtml()" class="btn btn-default" title="新建" ><i class="fa fa-file-o"></i> 新增规格选项</button>
             </div>
			 <table class="table table-bordered table-striped table-hover dataTable">
					<thead>
						<tr>
							<th class="sorting">规格选项</th>
							<th class="sorting">排序</th>
							<th class="sorting">操作</th>
						</tr>
					</thead>
                    <tbody id="specOptions">
                    </tbody>
			  </table>
		</div>
		<div class="modal-footer">						
			<button onclick="saveSpecification()" class="btn btn-success" data-dismiss="modal" aria-hidden="true">保存</button>
			<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
		</div>
	  </div>
	</div>
</div>
<!--修改窗口-->
  <div class="modal fade" id="updateModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  </div>
</body>
</html>
<script>
	$(function () {
		listSpecifications();
	})
	function listSpecifications() {
		$.ajax({
			url:"/specificationController/listSpecifications",
			data: {specName:$("#specNamee").val()},
			type:"post",
			dataType:"json",
			async:false,
			success:function (result) {
				if (result.code==10000){
					var html = ''
					var specifications = result.data;
					for (var i = 0;i<specifications.length;i++){
						html += '<tr >'
						html += '<td><input name="box" value="'+specifications[i].id+'" type="checkbox" ></td>'
						html += '<td>'+specifications[i].id+'</td>'
						html += '<td>'+specifications[i].specName+'</td>'
						html += '<td class="text-center">'
						html += '<button type="button" class="btn bg-olive btn-xs" data-toggle="modal" onclick="toUpdate('+specifications[i].id+')" data-target="#updateModal">修改</button>'
						html += '</td>'
						html += '</tr>'
					}
					$("#listSpecifications").html(html);
				}else {
					result.message
				}
			},
			error:function (result) {
				alert("系统错误")
			}
		})
	}
	function toUpdate(id) {
		$.ajax({
			url:"/specificationController/selectSpecificationAndOptionById",
			data:{id:id},
			dataType:"json",
			type:"post",
			async:false,
			success:function (result) {
				if (result.code==10000){
					var specifications = result.data;
					var options = specifications.options;
					var html = ''
					html+='<div class="modal-dialog" >'
					html+='<div class="modal-content">'
					html+='<div class="modal-header">'
					html+='<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>'
					html+='<h3 id="myModalLabel">规格编辑</h3>'
					html+='</div>'
					html+='<div class="modal-body">'
					html+='<table class="table table-bordered table-striped"  width="800px">'
					html+='<tr>'
					html+='<td>规格名称</td>'
					html+='<td><input id="specName2" value="'+specifications.specName+'" class="form-control" placeholder="规格名称">  </td>'
					html+='</tr>'
					html+='</table>'
					html+='<!-- 规格选项 -->'
					html+='<div class="btn-group">'
					html+='<button type="button" onclick="addSpecOptionHtml2()" class="btn btn-default" title="新建" ><i class="fa fa-file-o"></i> 新增规格选项</button>'
					html+='</div>'
					html+='<table class="table table-bordered table-striped table-hover dataTable">'
					html+='<thead>'
					html+='<tr>'
					html+='<th class="sorting">规格选项</th>'
					html+='<th class="sorting">排序</th>'
					html+='<th class="sorting">操作</th>'
					html+='</tr>'
					html+='</thead>'
					html+='<tbody id="specOptions2">'
					for (var i= 0;i<options.length;i++){
						html+='<tr>'
						html+='<td>'
						html+='<input value="'+options[i].optionName+'" class="form-control" placeholder="规格选项">'
						html+='</td>'
						html+='<td>'
						html+='<input value="'+options[i].orders+'" class="form-control" placeholder="排序">'
						html+='</td>'
						html+='<td>'
						html+='<button type="button" onclick="delThis(this)" class="btn btn-default" title="删除" ><i class="fa fa-trash-o"></i> 删除</button>'
						html+='</td>'
						html+='</tr>'
					}
					html+='</tbody>'
					html+='</table>'
					html+='</div>'
					html+='<div class="modal-footer">'
					html+='<button onclick="updateSpecification('+specifications.id+')" class="btn btn-success" data-dismiss="modal" aria-hidden="true">保存</button>'
					html+='<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>'
					html+='</div>'
					html+='</div>'
					html+='</div>'
					$("#updateModal").html(html);
				}
			},
			error:function (result) {
				alert("系统错误")
			}
		})
	}
	function saveSpecification() {
		var specName= $("#specName").val();
		var trList =$("#specOptions").find('tr');//通过id选择器找到tr标签,然后成为集合
		var arr = [];
		$(trList).each(function (i,e) {//使用循环遍历tr标签
			var tdList = $(e).find('td')//通过tr元素找到里面的td标签
			var optionName = $(tdList[0]).find('input').val();//通过td标签集合的第一位找到input框,然后获取里面的val值
			var orders = $(tdList[1]).find('input').val();
			var options ={optionName:optionName,orders:orders}//将得到的值变成json格式
			arr.push(options);//将这个json格式添加到数组里
		})
		//js将对象转成json字符串
		var optionList= JSON.stringify(arr);
		$.ajax({
			url:"/specificationController/addSpecification",
			data:{specName:specName,optionList:optionList},
			dataType:"json",
			type:"post",
			async:false,
			success:function (result) {
				if (result.code==10000){
					alert(result.message)
				}else {
					alert(result.message)
				}
			},
			erroe:function (result) {
				alert("系统错误")
			}
		})
		console.log(trList);
	}
	function updateSpecification(id) {
		var specName= $("#specName2").val();
		var trList =$("#specOptions2").find('tr');//通过id选择器找到tr标签,然后成为集合
		var arr = [];
		$(trList).each(function (i,e) {//使用循环遍历tr标签
			var tdList = $(e).find('td')//通过tr元素找到里面的td标签
			var optionName = $(tdList[0]).find('input').val();//通过td标签集合的第一位找到input框,然后获取里面的val值
			var orders = $(tdList[1]).find('input').val();
			var options ={optionName:optionName,orders:orders}//将得到的值变成json格式
			arr.push(options);//将这个json格式添加到数组里
		})
		//js将对象转成json字符串
		var optionList= JSON.stringify(arr);
		$.ajax({
			url:"/specificationController/updateSpecification",
			data:{specName:specName,optionList:optionList,id:id},
			dataType:"json",
			type:"post",
			async:false,
			success:function (result) {
				if (result.code==10000){
					alert(result.message)
				}else {
					alert(result.message)
				}
			},
			erroe:function (result) {
				alert("系统错误")
			}
		})
		console.log(trList);
	}

	function addSpecOptionHtml() {
		var html = ''
		html += '<tr>'
		html += '<td>'
		html += '<input  class="form-control" placeholder="规格选项">'
		html += '</td>'
		html += '<td>'
		html += '<input  class="form-control" placeholder="排序">'
		html += '</td>'
		html += '<td>'
		html += '<button type="button" onclick="delThis(this)" class="btn btn-default" title="删除" ><i class="fa fa-trash-o"></i> 删除</button>'
		html += '</td>'
		html += '</tr>'
		$("#specOptions").append(html)
	}
	function delThis(gf){
		var ll=confirm("确定删除吗？");
		if(ll){
			var mm=gf.parentNode.parentNode;//获取上一个节点,上一个节点,就是本节点的爷爷
			mm.parentNode.removeChild(mm);//爷爷的爷爷删除爷爷
		}
	}
	function addSpecOptionHtml2() {
		var html = ''
		html += '<tr>'
		html += '<td>'
		html += '<input  class="form-control" placeholder="规格选项">'
		html += '</td>'
		html += '<td>'
		html += '<input  class="form-control" placeholder="排序">'
		html += '</td>'
		html += '<td>'
		html += '<button type="button" onclick="delThis(this)" class="btn btn-default" title="删除" ><i class="fa fa-trash-o"></i> 删除</button>'
		html += '</td>'
		html += '</tr>'
		$("#specOptions2").append(html)
	}
	function delThis(gf){
		var ll=confirm("确定删除吗？");
		if(ll){
			var mm=gf.parentNode.parentNode;//获取上一个节点,上一个节点,就是本节点的爷爷
			mm.parentNode.removeChild(mm);//爷爷的爷爷删除爷爷
		}
	}
	function delSpecificatons() {
		alert("要删除吗");
		var boxs = document.getElementsByName("box");
		var ids = "";
		for (var i = 0;i<boxs.length;i++){
			if (boxs[i].checked){
				ids+=boxs[i].value+","
			}
		}
		ids = ids.substring(0,ids.length-1);
		$.ajax({
			url:"/specificationController/delSpecificatons",
			data:{ids:ids},
			dataType:"json",
			type:"post",
			async:false,
			success:function (result){
				if (result.code==10000){
					alert("删除成功");
					window.location.reload();
				}else {
					alert("删除失败")
				}
			},
			error:function (){
				alert("系统错误");
			}
		})
	}
</script>